//
//  AccountTools.m
//  AppExample
//
//  Created by user on 15/10/16.
//  Copyright © 2015年 AppExample. All rights reserved.
//

#import "AccountTools.h"
#import <CommonCrypto/CommonDigest.h>

@implementation AccountTools

//转化为json串
+(NSData *)toJSONData:(id)theData
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] != 0 && error == nil){
        return jsonData;
    }else{
        return nil;
    }
}

//判断输入是否为空
+(BOOL)isEmpty:(NSString *) string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    if ([string isEqualToString:@"null"]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]) {
        return YES;
    }
    if ([string isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

+(BOOL)isLogin
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userToken = [userDefaults objectForKey:@"token"];
    if ([self isEmpty:userToken]) {
        return NO;
    }else{
        return YES;
    }
}
/**
 *  登录之后将token存入本地方便以后接口调用
 *
 *  @param token 登录之后后台返回的一串字符
 */
+(void)saveUserToken:(NSString*)token
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:token forKey:@"token"];
    [userDefaults  synchronize];
}

+(NSString*)getUserToken
{
    return [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
}
//从NSUserDefaults中删除
+ (void)removeNSUserToken
{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    [userDefaultes removeObjectForKey:@"token"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

+ (BOOL)validatePassWord:(NSString*)PassWord
{
    if (PassWord.length>32||PassWord.length<8) {
        return NO;
    }else{
        return YES;
    }
}
+ (BOOL)validatePhoneNumber:(NSString *)string{
    
    NSString *phoneRegex = @"^(1[0-9])\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:string];
}
+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);// This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
+(void)saveUserCd:(NSString *)userCd
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userCd forKey:@"userCd"];

}
+(NSString *)getUserCd
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userCd = [userDefaults objectForKey:@"userCd"];
    return userCd;

}
+ (void)Saveid:(NSString *)userid userHeadPhoto:(NSString *)userHeadPhoto
{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:userid forKey:@"userId"];
    [userDefaults setObject:userHeadPhoto forKey:@"userHeadPhoto"];
    [userDefaults synchronize];
}

+ (BOOL)allIsNumber:(NSString *)string{
    NSString *numberRegex = @"^[0-9]*$";
    NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
    return [numberTest evaluateWithObject:string];
}
+ (BOOL)validateEmail:(NSString *)string{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:string];
}
+ (void)exitLogin
{
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"token"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userCd"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userHeadPhoto"];
//    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"jobNumber"];
     [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userType"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


@end
